package com.xunker.appointment.service.impl;



import com.alibaba.fastjson.JSONObject;
import com.xunker.appointment.exception.AppointmentException;
import com.xunker.appointment.mapper.OrderInfoMapper;
import com.xunker.appointment.mapper.ScheduleMapper;
import com.xunker.appointment.model.orderinfo.OrderInfo;
import com.xunker.appointment.model.patient.Patient;
import com.xunker.appointment.model.schedule.Schedule;
import com.xunker.appointment.result.ResultCodeEnum;
import com.xunker.appointment.service.HospitalService;
import com.xunker.appointment.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Autowired
    private OrderService orderService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> submitOrder(Map<String, Object> paramMap) {
        log.info(JSONObject.toJSONString(paramMap));
        String hospitalCode = (String) paramMap.get("hospitalCode");
        String departmentCode = (String) paramMap.get("departmentCode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        String reserveDate = (String) paramMap.get("reserveDate");
        String reserveTime = (String) paramMap.get("reserveTime");
        String amount = (String) paramMap.get("amount");
        Schedule schedule=this.getSchedule(hosScheduleId);
        if (null==schedule) {
            throw new AppointmentException(ResultCodeEnum.DATA_ERROR);
        }
        if (!schedule.getHospitalCode().equals(hospitalCode)||!schedule.getDepartmentCode().equals(departmentCode)||!schedule.getAmount().toString().equals(amount)) {
            throw new AppointmentException(ResultCodeEnum.DATA_ERROR);
        }
        String s = JSONObject.toJSONString(paramMap);
        //就诊人信息
        Patient patient = JSONObject.parseObject(s, Patient.class);
        log.info(JSONObject.toJSONString(patient));
        //处理就诊人业务
        Long patientId=this.savePatient(patient);
        Map<String, Object> resultMap = new HashMap<>();
        int availableNumber = schedule.getAvailableNumber().intValue() - 1;
        if (availableNumber>0) {
            schedule.setAvailableNumber(availableNumber);
            scheduleMapper.updateById(schedule);
            //记录预约记录
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setPatientId(patientId);
            orderInfo.setScheduleId(hosScheduleId);
            int number = schedule.getReservedNumber().intValue() - schedule.getAvailableNumber().intValue();
            orderInfo.setNumber(number);
            orderInfo.setAmount(new BigDecimal(amount));
            String fetchTime = "0".equals(reserveDate) ? " 09:30前" : " 14:00前";
            orderInfo.setFetchTime(reserveTime+fetchTime);
            orderInfo.setFetchAddress("一楼9号窗口");
            //默认 未支付
            orderInfo.setOrderStatus(0);
            orderService.save(orderInfo);
            resultMap.put("resultCode","0000");
            resultMap.put("resultMsg","预约成功");
            //预约记录唯一标识（医院预约记录主键）
            resultMap.put("hosRecordId",orderInfo.getId());
            //预约序号
            resultMap.put("number",number);
            //取号时间
            resultMap.put("fetchTime",reserveDate+"09:00前");
            //取号地点
            resultMap.put("fetchAddress","一层114窗口");
            //排班可预约数
            resultMap.put("reservedNumber",schedule.getReservedNumber());
            //排班剩余预约数
            resultMap.put("availableNumber",schedule.getAvailableNumber());
        }else {
            throw new AppointmentException(ResultCodeEnum.DATA_ERROR);
        }
        return resultMap;
    }

    private Long savePatient(Patient patient) {
        return 1L;
    }

    private Schedule getSchedule(String hosScheduleId) {
        return scheduleMapper.selectById(hosScheduleId);
    }

    @Override
    public void updatePayStatus(Map<String, Object> paramMap) {
        String hospitalCode = (String) paramMap.get("hospitalCode");
        String hosRecordId = (String) paramMap.get("hosRecordId");
        OrderInfo orderInfo = orderService.getById(hosRecordId);
        if (null==orderInfo) {
            throw new AppointmentException(ResultCodeEnum.DATA_ERROR);
        }
        //已支付
        orderInfo.setOrderStatus(1);
        //orderInfo.setPayTime(new Date());
        orderService.updateById(orderInfo);
    }

    @Override
    public void updateCancelStatus(Map<String, Object> paramMap) {
        String hospitalCode = (String) paramMap.get("hospitalCode");
        String hosRecordId = (String) paramMap.get("hosRecordId");
        OrderInfo orderInfo=orderService.getById(hosRecordId);
        if (null==orderInfo) {
            throw new AppointmentException(ResultCodeEnum.DATA_ERROR);
        }
        //已取消
        orderInfo.setOrderStatus(-1);
        orderInfo.setQuitTime(new Date());
        orderService.updateById(orderInfo);
    }
}
